class FetionMessage::BuddyMessage < FetionMessage
  
#  def after_save
#    do_it_even_exception do
#      if not ( message =~ /不在/ or message =~ /离开/ )
#        if user.present?
#          money_types = message.split(' ').each_with_object([]) do |key_word,money_types|
#            logger.debug "开始根据关键字【#{key_word}】检索币种"
#            MoneyType.find_all_by_key_word(key_word).each do |money_type|
#              money_types << money_type if(money_type.present? and not money_types.include?(money_type))
#            end
#          end
#          if money_types.any?
#            money_types.each do |money_type|
#              logger.debug "汇率查询： #{money_type.name}"
#              money_type.send_sm_to user
#            end
#          else
#            user.send_sm_by_lite_fetion('你好，我目前只能够向您提供汇率查询。只需要您向我发送含币种的关键字（之间用空格间隔）的短信即可，支持多个币种查询哦。例如您回复短信"韩 美 日" 或 "han mei riben" ，我将会告诉您中行公布的最新的韩元、美元、日元汇率。如有更好的建议或者点子请直接与麦吐司管理员QQ 7861848对话 ：）')
#          end
#        end
#      end
#    end
#  end

  SEARCH_AREA_WEATHER_INFO_KEY_WORDS = ['天气','tianqi','tian qi','tian_qi','tq']

  SEARCH_AREA_WEATHER_INFO_KEY_WORDS_REGULAR_EXPRESSION = class_eval("/#{SEARCH_AREA_WEATHER_INFO_KEY_WORDS.join('|')}/")

  SEARCH_FOREIGN_EXCHANGE_RATE_KEY_WORDS = ['汇率','huilv','hl','hui lv','hui_lv']

  SEARCH_FOREIGN_EXCHANGE_RATE_KEY_WORDS_REGULAR_EXPRESSION = class_eval("/#{SEARCH_FOREIGN_EXCHANGE_RATE_KEY_WORDS.join('|')}/")

  SEARCH_MTS_URL_KEY_WORDS = ['网址','域名','网页','打不开','访问','网站','怎么']

  SEARCH_MTS_URL_KEY_WORDS_REGULAR_EXPRESSION = class_eval("/#{SEARCH_MTS_URL_KEY_WORDS.join('|')}/")

  def action
    @action
  end

  def money_types
    @money_types
  end

  def areas
    @areas
  end

  def after_save
    do_it_even_exception do
      case
      when message =~ /不在|离开/
        @action = :do_nothing
        do_nothing
      when message.downcase =~ FetionMessage::BuddyMessage::SEARCH_AREA_WEATHER_INFO_KEY_WORDS_REGULAR_EXPRESSION
        @action = :searh_area_weather_infos
        search_area_weather_infos
      when message.downcase =~ FetionMessage::BuddyMessage::SEARCH_FOREIGN_EXCHANGE_RATE_KEY_WORDS_REGULAR_EXPRESSION
        @action = :searh_foreign_exchange_rates
        search_foreign_exchange_rates
      when message.downcase =~ FetionMessage::BuddyMessage::SEARCH_MTS_URL_KEY_WORDS_REGULAR_EXPRESSION
        @action = :search_mts_url
        search_mts_url
      else
        @action = :helps
        helps
      end if user.present?
    end
  end

  def do_nothing
  end

  def search_foreign_exchange_rates    
    @money_types = message.downcase.gsub(FetionMessage::BuddyMessage::SEARCH_FOREIGN_EXCHANGE_RATE_KEY_WORDS_REGULAR_EXPRESSION,'').split(' ').each_with_object([]) do |key_word,money_types|
      logger.debug "开始根据关键字【#{key_word}】检索币种"
      MoneyType.find_all_by_key_word(key_word).each do |money_type|
        money_types << money_type if(money_type.present? and not money_types.include?(money_type))
      end
    end
    if @money_types.any?
      @money_types.each do |money_type|
        logger.debug "汇率查询： #{money_type.name}"
        money_type.send_sm_to user
      end
    else
      user.send_sm_by_lite_fetion('您好，我目前只能够向您提供汇率查询。只需要您向我发送 汇率(hl或huilv) + 含币种的关键字（之间用空格间隔）的短信即可，支持多个币种查询哦。例如您回复短信"汇率 韩 美 日" 或 "hl han mei riben" ，我将会告诉您中行公布的最新的韩元、美元、日元汇率。如有更好的建议或者点子请直接与麦吐司管理员QQ 7861848对话 ：）')
    end
  end

  def search_area_weather_infos
    @areas = message.downcase.gsub(FetionMessage::BuddyMessage::SEARCH_AREA_WEATHER_INFO_KEY_WORDS_REGULAR_EXPRESSION,'').split(' ').each_with_object([]) do |key_word,areas|
      logger.debug "开始根据关键字查询【#{key_word}】城市天气"
      Area.find_all_by_key_word(key_word).each do |area|
        areas << area if (area.present? and not areas.include?(area))
      end
    end
    if @areas.any?
      @areas.each do |area|
        area.send_sm_to user
      end
    else
      user.send_sm_by_lite_fetion('回复"天气 城市名关键字" 如："天气 北京 三亚" 或 "tq beijing  sanya" 或 "tq 北京 sanya" 将返回北京和三亚明日的天气预报 ')
    end
    
  end

  def search_mts_url
    user.send_sm_by_lite_fetion('您好，麦吐司网的网址是www.mytusi.com')
  end

  def helps
    user.send_sm_by_lite_fetion('您好，我是麦吐司机器人.目前能够向您提供天气,汇率,网址实时查询.请回复"天气"，"汇率"或"网址"获得详细帮助：）')
  end
end